package wiki_playground_dimitar;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathFactoryConfigurationException;
import javax.xml.xpath.XPathFunctionResolver;
import javax.xml.xpath.XPathVariableResolver;

import com.sun.org.apache.xml.internal.security.transforms.params.XPathContainer;
import com.sun.org.apache.xpath.internal.compiler.XPathDumper;
import com.sun.xml.internal.bind.v2.runtime.output.Encoded;

import processing.core.PApplet;
import processing.xml.XMLElement;
//import proxml.XMLInOut;


public class WikiApi_003 extends PApplet {
	int depth;
	XMLElement xmlE;
	XMLElement xmlCategoryTree;
	
	File f;
	FileReader fr;
	String[] queries = {
			"http://en.wikipedia.org/w/api.php?action=query&format=xml&prop=categories&cllimit=400&titles=Albert%20Einstein",
			"http://en.wikipedia.org/w/api.php?action=query&format=xml&list=allcategories&aclimit=500&acprop=size",
			 "http://en.wikipedia.org/w/api.php?action=query&generator=allcategories&format=xml&gacprefix=List&prop=info",
			"http://en.wikipedia.org/w/api.php?action=query&format=xml&list=categorymembers&cmtitle=Category:Physics",
			"http://en.wikipedia.org/w/api.php?action=query&format=xml&list=categorymembers&cmprop=ids|type|title&cmlimit=500&cmtitle=Category:Contents"
	};
	String catQueryBase = "http://en.wikipedia.org/w/api.php?action=query&format=xml&list=categorymembers&cmprop=ids|type|title&cmlimit=500&cmtitle=";
	
	
	public void setup() {
		size(800,800);
		//String s0 = toURL("Category:Contents");
		String s0 = "Category:Contents";
		xmlE = new XMLElement(this,catQueryBase+s0);
//		for (int i = 0; i < depth; i++) {
//			xmlE = new XMLElement(this,catQueryBase+s0);
//			XMLElement[] children = xmlE.getChildren("query/categorymembers");
//			for (int j = 0; j < children.length; j++) {
//				
//			}
//		}
		
//		XMLElement[] x = xmlE.getChildren("query/categorymembers");
//		for (int i = 0; i<x[0].getChildCount() ; i++) {
//			System.out.println(x[0].getChild(i).getAttribute("title"));
//		}
	//	System.out.println(x[0].getChild(1).getAttribute("title"));
	//	System.out.println(x[0].getChild(1));
	//	System.out.println(xmlE.getChild(0));
		depth = 2;
		printQueryChildren(catQueryBase, s0, "query/categorymembers", "title", "type", "subcat", depth);
		
	}
	
	public void draw() {
	}

	private void printQueryChildren(String queryBase, String queryTerm, String theTag, String theAttribute, String theTypeAttrib, String theTypeValue, int theDepth) {
		if (theDepth == 0) {
			//System.out.println("***************");
			//System.out.println("reached bottom!");
			return;
		}
		//queryTerm = toURL(queryTerm);
		XMLElement theXMLElement  =  new XMLElement(this,queryBase+queryTerm);
		XMLElement[] children = theXMLElement.getChildren(theTag);
		
		for (int i = 0; i < children[0].getChildCount(); i++) {
			///speichern?
			String s0 = children[0].getChild(i).getAttribute(theAttribute);
			for (int j = 0; j <this.depth-theDepth; j++) {
				System.out.print(">");
			}
			System.out.println(s0);
			s0 = toURL(s0);
			if (children[0].getChild(i).getAttribute(theTypeAttrib).equalsIgnoreCase(theTypeValue)) {
			printQueryChildren(queryBase, s0, theTag, theAttribute, theTypeAttrib, theTypeValue, theDepth-1);
			}
		}
	}
	
	
	private void printChildren(XMLElement theXMLElement, String theTag, String theAttribute, String theTypeAttrib, String theTypeValue, int depth) {
		if (depth == 0) return;
		XMLElement[] children = theXMLElement.getChildren(theTag);
		for (int i = 0; i < theXMLElement.getChildCount(); i++) {
			///speichern?
			System.out.println(children[i].getAttribute(theAttribute));
			if (children[i].getAttribute(theTypeAttrib) == theTypeValue) {
			printChildren(children[i], theTag, theAttribute, theTypeAttrib, theTypeValue, depth-1);
			}
		}
	}
	
	private String toURL(String s) {
		try {
			String s2 = URLEncoder.encode(s, "UTF-8");
			return s2;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
}
